package com.online.edu.course.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.online.edu.course.entity.CategoryMapping;
import com.online.edu.course.entity.Vo.CourseREQ;
import com.online.edu.course.entity.Vo.CourseVo;
import com.online.edu.course.entity.Vo.PageVo;
import com.online.edu.course.entity.Vo.QueryCourseVo;
import com.online.edu.course.entity.Course;
import com.online.edu.course.service.ICategoryMappingService;
import com.online.edu.course.service.ICategoryService;
import com.online.edu.course.service.ICourseService;
import com.online.edu.util.core.Result;
import com.online.edu.util.utils.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * <p>
 * 课程表 前端控制器
 * </p>
 *
 * @author world
 * @since 2021-07-30
 */
@Api(tags = "课程表接口")
@RestController
@RequestMapping("/course/course")
public class CourseController {

    @Autowired
    ICourseService courseService;

    @Autowired
    ICategoryService categoryService;

    @Autowired
    ICategoryMappingService categoryMappingService;

    //课程logo上传
    @ApiOperation("获取课程LogoUrl")
    @PostMapping("/getLogoUrl")
    public Result getLogoUrl(MultipartFile file){
        String courseLogo = courseService.uploadFile(file);
        return Result.ok(courseLogo);
    }

    //课程目录
    @ApiOperation("分页获取课程目录")
    @PostMapping("/list")
    public Result<List<CourseREQ>> list(@RequestBody PageVo pageVo){
        List<CourseREQ> courseREQS = courseService.listPage(pageVo);
        return Result.ok(courseREQS);
    }

    //课程查询
    @ApiOperation("多条件课程查询")
    @PostMapping("/query")
    public Result<List<CourseREQ>> query(@RequestBody QueryCourseVo queryCourseVo){
        QueryWrapper<Course> queryWrapper = new QueryWrapper<>();
        //动态sql
        if (!StringUtils.isEmpty(queryCourseVo.getCourseName())){
            queryWrapper.like("course_name", queryCourseVo.getCourseName());
        }
        if (!StringUtils.isEmpty(queryCourseVo.getStatusId())){
            queryWrapper.eq("status_id", queryCourseVo.getStatusId());
        }
        if (!StringUtils.isEmpty(queryCourseVo.getIsFree())){
            queryWrapper.eq("is_free", queryCourseVo.getIsFree());
        }
        if (!StringUtils.isEmpty(queryCourseVo.getIsPutaway())){
            queryWrapper.eq("is_putaway", queryCourseVo.getIsPutaway());
        }
        PageVo pageVo1 = new PageVo();
        BeanUtils.copyProperties(queryCourseVo, pageVo1);
        List<CourseREQ> courseList = courseService.listByWrapper(pageVo1,queryWrapper);

        return Result.ok(courseList);
    }

    //重置操作
    @ApiOperation("重置操作")
    @GetMapping("/reset")
    public Result<List<CourseREQ>> reset(QueryCourseVo queryCourseVo){
        queryCourseVo.setCourseName(null);
        queryCourseVo.setIsFree(null);
        queryCourseVo.setIsPutaway(null);
        queryCourseVo.setStatusId(null);
        PageVo pageVo = new PageVo();
        List<CourseREQ> courseREQS = courseService.listPage(pageVo);
        return Result.ok(courseREQS);

    }

    //判断新增or更新
    @ApiOperation("判断新增/更新")
    @PostMapping("/save")
    public Result save(@RequestBody CourseVo courseVo){
        Course course = courseVo.getCourse();
        long categoryId = courseVo.getCategoryId();
        //判断id，如果id为空则新增，否则修改
        if (course.getId() != null){
            boolean b = courseService.updateById(course);
            UpdateWrapper<CategoryMapping> updateWrapper = new UpdateWrapper<>();
            updateWrapper.eq("course_id",course.getId());
            updateWrapper.set("category_id",categoryId);
            boolean a =categoryMappingService.update(updateWrapper);
            if (b&a) {
                return Result.success("更新成功！");
            }
            else {
                return Result.error("更新失败！");
            }
        }
        else {
            course.setId(Long.valueOf(StringUtil.uuid()));
            boolean save = courseService.save(course);
            if (save) {
                courseService.addCategoryMapping(categoryId,course.getId());//添加关联表映射
                courseService.addCategoryNums(categoryId);//如果新增成功则对应的分类数量+1
                return Result.success("新增成功！");
            }
            else {
                return Result.error("新增失败！");
            }
        }
    }

    //上下架
    @ApiOperation("上下架控制")
    /*@PostMapping("/putaway/{id}")
    public Result putaway(@PathVariable("id") long id)*/
    @PostMapping("/putaway/{id}")
    public Result putaway(@PathVariable("id") long id){
        //获取当前上下架的状态
        Integer isPutaway = courseService.getById(id).getIsPutaway();
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        if (isPutaway == 0){
            updateWrapper.eq("id",id).set("is_putaway",1);
        }
        if (isPutaway == 1){
            updateWrapper.eq("id",id).set("is_putaway",0);
        }
        boolean b = courseService.update(updateWrapper);
        if (b) {
            return Result.success("更新成功！");
        }
        else {
            return Result.error("更新失败！");
        }
    }

    //状态
    @ApiOperation("状态控制")
    @PostMapping("/status/{id}")
    public Result status(@PathVariable("id") long id){
        //获取当前上下架的状态
        Course course = courseService.getById(id);
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        if (course.getStatusId() == 0){
            updateWrapper.eq("id",id).set("status_id",1);
        }
        if (course.getStatusId() == 1){
            updateWrapper.eq("id",id).set("status_id",0);
        }
        boolean b = courseService.update(course,updateWrapper);
        if (b) {
            return Result.success("更新成功！");
        }
        else {
            return Result.error("更新失败！");
        }
    }


    //删除列表
    @ApiOperation("通过ID删除列表")
    @PostMapping("/delete/{id}")
    public Result delete(@PathVariable("id") long id){
        Course course = courseService.getById(id);
        UpdateWrapper<Course> updateWrapper = new UpdateWrapper<>();
        //此处使用逻辑删除
        updateWrapper.eq("id",id).set("logical_state",1);
        boolean b = courseService.update(course,updateWrapper);
        if (b) {
            return Result.success("删除成功！");
        }
        else {
            return Result.error("删除失败！");
        }
    }
}
